/*
 * CDataStore.h
 *
 *  Created on: 2018年5月11日
 *      Author: zhengboyuan
 */

#ifndef CDATASTORE_H_
#define CDATASTORE_H_

#include "DataStore.h"
#include "SqliteStore.h"
#include "TCriticalSection.h"


class CDataStore: public DataStore
{
public:
	static std::string toJsonString(int offset, int limit, int total, JsonList& items);

public:
	CDataStore();
	virtual ~CDataStore();

	virtual bool open(const std::string& path);

	virtual void close();

	virtual bool isOpen();

    virtual bool execute(const std::string& sql);

    virtual bool query(const std::string& sql, std::string& value);

    virtual bool query(const std::string& sql, int& value);

    virtual int update(const std::string& sql);

    virtual bool query(const std::string& sql, Json::Value& json);

    virtual bool query(const std::string& sql, JsonList& jsonList);

    virtual bool queryToJson(const std::string& sql, std::string& text);
    virtual bool querySomeToJson(const std::string& sql, std::string& text);

    virtual bool update(const std::string& table, const Json::Value& json, const std::string& condition);

    virtual bool insert(const std::string& sql, const std::string& table, long& id);
    virtual bool insert(const std::string& table, const Json::Value& json);
    virtual bool insert(const std::string& table, const Json::Value& json, long& id);

    virtual bool replace(const std::string& table, const Json::Value& json);

    virtual bool search(const std::string& table, const std::string& query, const std::string& condition, const std::string& order,
    			int offset, int limit, int& total, JsonList& jsonList);


	virtual bool addEvent(EventRecord& record);

	virtual bool removeEvent(int id);

	virtual bool getEvent(int id, EventRecord& record);

	virtual bool updateEvent(EventRecord& record);


	virtual bool addImage(ImageRecord& record);

	virtual bool removeImage(int id);

	virtual bool getImage(int id, ImageRecord& record);


	virtual bool findEvent(const char* filepath, EventRecord& record);

	virtual bool findImage(const char* filepath, ImageRecord& record);


	virtual int searchEvent(const char* company, const char* station, int num, time_t fromTime, time_t toTime, EventRecord* record, int size);

	virtual bool removeEvents(const char* company, const char* station, int num, time_t fromTime, time_t toTime);

	virtual int searchImage(const char* company, const char* station, int num, time_t fromTime, time_t toTime, ImageRecord* record, int size);

	virtual bool removeImages(const char* company, const char* station, int num, time_t fromTime, time_t toTime);


protected:

    int queryInt(const std::string& sql, int defValue=0);

    bool insert(const std::string& sql, int& autoId);

    bool insert(const std::string& sql, int64_t& autoId);

    template < class T >
    bool queryObject(const std::string& sql, T& t)
    {
    	comn::AutoCritSec lock(m_cs);
    	return m_store.queryObject(sql, t);
    }

    template < class Container >
    int queryObjects(const std::string& sql, Container& cont)
    {
    	comn::AutoCritSec lock(m_cs);
    	return m_store.queryObjects(sql, cont);
    }

	template < class Record >
	int queryObjects(const std::string& sql, Record* records, int size)
	{
		comn::AutoCritSec lock(m_cs);
		return m_store.queryObjects(sql, records, size);
	}

protected:
	SqliteStore	m_store;
	comn::CriticalSection	m_cs;

};

#endif /* CDATASTORE_H_ */
